<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
          <el-input v-model="query.stepsCode" clearable placeholder="工步集编码" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
          <el-input v-model="query.stepsName" clearable placeholder="工步集名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
          <el-select
            v-model="query.productCode"
            clearable
            filterable
            placeholder="产品编码"
            style="width: 185px;"
            class="filter-item"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in products"
              :key="item.id"
              :label="item.productName"
              :value="item.productCode"
            >
              <span style="float: left">{{ item.productName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.productCode }}</span>
            </el-option>
          </el-select>
          <el-select
            v-model="query.isDisable"
            clearable
            filterable
            placeholder="是否禁用"
            style="width: 185px;"
            class="filter-item"
            @change="crud.toQuery"
          >
            <el-option
              v-for="item in dict.is_disable"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="550px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
          <el-form-item label="工步集编码" prop="stepsCode">
            <el-input v-model="form.stepsCode" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="工步集名称" prop="stepsName">
            <el-input v-model="form.stepsName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="关联产品" prop="productCode">
            <el-select v-model="form.productCode" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in products"
                :key="item.id"
                :label="item.productName"
                :value="item.productCode" >
                <span style="float: left">{{ item.productName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.productCode }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="版本" prop="version">
            <el-input v-model="form.version" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否禁用" prop="isDisable">
            <el-radio-group v-model="form.isDisable">
              <el-radio :label="item.value" v-for="item in dict.is_disable">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
<!--      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="stepsCode" label="工步集编码" width="200px;" align="center"/>
        <el-table-column prop="stepsName" label="工步集名称" width="200px;" align="center"/>
        <el-table-column prop="productCode" label="产品编码" width="200px;" align="center"/>
        <el-table-column prop="version" label="版本" width="100px;" align="center"/>
        <el-table-column prop="isDisable" label="是否禁用" width="100px;" align="center">
          <template slot-scope="scope">
            <el-tag type="success" size="small" v-if="scope.row.isDisable == '0'">{{ dict.label.is_disable[scope.row.isDisable] }}</el-tag>
            <el-tag type="info" size="small" v-if="scope.row.isDisable == '1'">{{ dict.label.is_disable[scope.row.isDisable] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createBy" label="创建人" width="100px;" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="150px;" align="center"/>
        <el-table-column prop="updateBy" label="更新人" width="100px;" align="center"/>
        <el-table-column prop="updateTime" label="更新时间" width="150px;" align="center"/>
        <el-table-column prop="remark" label="描述" width="250px;" align="center"/>
        <el-table-column v-if="checkPer(['admin','craftStep:edit','craftStep:del'])" fixed="right" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission">
              <router-link slot="view" :to="'/craft/step/detail/' + scope.row.id">
                <el-button size="mini">  工步 </el-button>
              </router-link>
            </udOperation>
          </template>
        </el-table-column>
      </el-table>-->
      <el-empty
        v-show="!crud.data || crud.data.length == 0"
        description="暂无数据"
      ></el-empty>
      <!--渲染列表-->
      <el-row
        :gutter="20"
        v-show="crud.data && crud.data.length > 0"
        v-loading="crud.loading"
      >
        <el-col :span="6" v-for="(item, index) in crud.data" :key="item.id">
          <div class="grid-content bg-purple">
            <el-card class="box-card">
              <div class="top">
                <img src="@/assets/images/steps.png" class="img" />
                <div class="content">
                  <div class="titleName">{{ item.stepsName }}</div>
                  <div class="subTitle">工步集编号: {{ item.stepsCode }}</div>
                  <div class="subTitle">产品型号: {{ item.productCode }}</div>
                  <div class="subTitle">
                    是否禁用:
                    <el-tag size="small" type="danger" v-if="item.isDisable === '1'">{{
                        dict.label.is_disable[item.isDisable]
                      }}</el-tag>
                    <el-tag size="small" v-if="item.isDisable === '0'">{{
                        dict.label.is_disable[item.isDisable]
                      }}</el-tag>
                  </div>
                </div>
              </div>
              <div class="bottom">

                <el-button-group style="width: 85px;white-space:nowrap; ">

                  <router-link  :to="'/craft/step/detail/' + item.id">
                    <i class="el-icon-search button" title="查看"
                       style="cursor: pointer"></i>
                  </router-link>

                  <i class="el-icon-edit button"
                     v-permission="permission.edit" :loading="crud.status.cu === 2"  @click="crud.toEdit(item)"
                     style="cursor: pointer" title="编辑"></i>

                  <i class="el-icon-delete button"
                     style="cursor: pointer"
                     @click="toDelete(item)" title="删除"></i>
                </el-button-group>
                <div class="time">创建时间: {{ item.createTime }}</div>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCraftStep from '@/api/craft/craftStep'
import crudProductModel from '@/api/product/productModel'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, stepsCode: null, stepsName: null, productCode: null, version: null, isDisable: '1', remark: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'CraftStep',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['is_disable'],
  cruds() {
    return CRUD({
      title: '工艺工步',
      url: 'api/craftStep',
      idField: 'id',
      sort: 'id,desc',
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true,
        import: false
      },
      crudMethod: { ...crudCraftStep }})
  },
  data() {
    return {
      products: [],
      permission: {
        add: ['admin', 'craftStep:add'],
        edit: ['admin', 'craftStep:edit'],
        del: ['admin', 'craftStep:del']
      },
      rules: {
        stepsCode: [
          { required: true, message: '工步集合编码不能为空', trigger: 'blur' }
        ],
        stepsName: [
          { required: true, message: '工步集合名称不能为空', trigger: 'blur' }
        ],
        productCode: [
          {required: true, message: '关联产品不能为空', trigger: 'blur'}
        ],
        version: [
          {required: true, message: '版本不能为空', trigger: 'blur'}
        ],
        isDisable: [
          {required: true, message: '状态不能为空', trigger: 'blur'}
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'ID' },
        { key: 'stepsCode', display_name: '工步集合编码' },
        { key: 'stepsName', display_name: '工步集合名称' },
        { key: 'productCode', display_name: '产品编码' },
        { key: 'version', display_name: '版本' },
        { key: 'isDisable', display_name: '是否禁用' },
        { key: 'remark', display_name: '描述' },
        { key: 'createBy', display_name: '创建人' },
        { key: 'updateBy', display_name: '更新人' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 确认删除
    toDelete(data) {
      this.$confirm("确认删除该工步集吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.crud.doDelete(data);
        })
        .catch(() => {});
    }
  },
  mounted() {
    crudProductModel.list().then(res =>{
      this.products = res;
    });
  }
}
</script>

<style scoped>

  .header-bottom {
    padding-bottom: 10px;
  }
  .el-card {
    margin-bottom: 15px;
  }

  .top {
    width: 100%;
    margin-bottom: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    justify-content: flex-start;
    align-items: flex-start;
    font-family: PingFangSC-Regular;
  }

  .img {
    width: 74px;
    margin-right: 15px;
  }

  .content {
    overflow: hidden;
    flex: 1 1;
  }

  .titleName {
    font-size: 14px;
    color: #333;
    line-height: 22px;
    font-weight: 700;
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .subTitle {
    font-size: 12px;
    color: #555;
    line-height: 20px;
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .descWrapper {
    font-size: 12px;
    color: #555;
    line-height: 20px;
    margin-bottom: 8px;
    display: flex;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .descContent {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    justify-content: flex-start;
  }
  .bottom .time {
    font-size: 13px;
    margin-left: 8px;
    color: #999;
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
  }
  .bottom .button {
    font-size: 18px;
    padding: 0 2px;
  }
  .bottom .button:hover {
    color: #409eff;
  }
</style>
